import {request} from "../../utils/util";

Component({
  properties: {
    data: {
      type: Object
    }
  },
  options: {
    styleIsolation: 'apply-shared'
  },
  data: {
    openStatus: false,
    qrcodeStatus: false,
    showSelPay: false,
    payForm: {
      order_sn: '',
      pay_money: '',
    },
  },
  methods: {
    changeOpenStatus () {
      this.setData({
        openStatus: !this.data.openStatus
      })
    },
    // 取消订单
    cancelOrder() {
      wx.showModal({
        title: '提示',
        content: '确定取消当前订单吗？',
        success: (res) => {
          if (res.confirm) {
            wx.showLoading({
              title: '加载中..',
              mask: true
            })
            request({
              url: `/api/web/shop/orders/${this.data.data.id}`,
              method: 'DELETE',
              success: ({ code, message }) => {
                wx.hideLoading()
                if (code === 200) {
                  wx.showToast({
                    title: '取消成功！',
                    icon: 'success',
                    duration: 2000
                  })
                  this.triggerEvent('handle-success')
                } else {
                  wx.showToast({
                    title: message,
                    icon: 'none',
                    duration: 2000
                  })
                }
              }
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    },
    closeQrcode () {
      this.setData({
        qrcodeStatus: false
      })
    },
    showQrcode () {
      wx.showLoading({
        title: '加载中..',
        mask: true
      })
      request({
        url: `/api/web/qrcode`,
        params: {
          order_sn: this.data.data.order_sn,
          type: 'goods'
        },
        success: ({ code, data, message }) => {
          wx.hideLoading()
          if (code === 200) {
            this.setData({
              qrcodeUrl: data.url,
              qrcodeStatus: true
            })
          } else {
            wx.showToast({
              title: message,
              icon: 'none',
              duration: 2000
            })
          }
        }
      })
    },
    toPay () {
      let { order_sn, actual_payment } = this.data.data
      this.setData({
        payForm: {
          order_sn,
          pay_money: actual_payment,
        },
        showSelPay: true
      })
    },
    closePay() {
      this.setData({
        showSelPay: false
      })
    },
    paySuccess() {
      this.triggerEvent('handle-success')
      this.closePay()
    },
    refund () {
      wx.showModal({
        title: '提示',
        content: '确认退款吗？',
        cancelColor: '#B7B7B7',
        confirmColor: '#3C9C25',
        success (res) {
          if (res.confirm) {
            console.log('用户点击确定')
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    }
  }

})
